//
//  MoreComicViewController.m
//  Comic
//
//  Created by Ryan on 16/3/17.
//  Copyright © 2016年 程锦. All rights reserved.
//

#import "MoreComicViewController.h"
#import "DetailViewController.h"
#import "MoreComicModel.h"
#import "MoreComicTableViewCell.h"

#define URL @"http://api.kuaikanmanhua.com/v1/%@?limit=20&offset=0"
#define Data @"http://api.kuaikanmanhua.com/v1/topics?limit=20&offset=0&tag=%@"

@interface MoreComicViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *topics_TableView;

@property (nonatomic,strong)NSMutableArray *topics_Arr;

@end

@implementation MoreComicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = self.titleStr;
    
    [self createData];
    
    self.topics_TableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.view addSubview:self.topics_TableView];
    
    self.topics_TableView.delegate = self;
    
    self.topics_TableView.dataSource = self;
    
    [self.topics_TableView registerClass:[MoreComicTableViewCell class] forCellReuseIdentifier:NSStringFromClass([MoreComicTableViewCell class])];
    
    self.topics_TableView.rowHeight = 100;
    
    [self.topics_TableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(0);
        
    }];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    rightButton.frame = CGRectMake(0, 0, 18, 18);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    [rightButton addTarget:self action:@selector(clickleft:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createData{
    
        NSString *dataURL;
    
    if (self.titleData.length == 0) {
        
        dataURL = [NSString stringWithFormat:URL,self.action];
        
    }else{
        
        NSString *str = [NSString stringWithString: [self.titleData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        
        dataURL = [NSString stringWithFormat:Data,str];
    }

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    
    [manager GET:dataURL parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = [NSArray arrayWithArray:responseObject[@"data"][@"topics"]];
        
        self.topics_Arr = [NSMutableArray array];
        
        
        for (NSDictionary *dic in arr) {
            
            MoreComicModel *model = [[MoreComicModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.topics_Arr addObject:model];
        }
        
        
        [self.topics_TableView reloadData];
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
    return self.topics_Arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MoreComicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MoreComicTableViewCell class])];
    
    MoreComicModel *model = self.topics_Arr[indexPath.row];
    
    cell.selectionStyle = NO;
    
    [cell.mainImageView setImageWithURL:[NSURL URLWithString:model.cover_image_url] placeholderImage:[UIImage imageNamed:@"placeholder_pic"]];
    
    cell.title.text = model.title;
    
    NSDictionary *dic = model.user;
    cell.nickname.text = dic[@"nickname"];
    
    [cell.likes setImage:[UIImage imageNamed:@"like"]];
    
    [cell.comments setImage:[UIImage imageNamed:@"chat_Gray"]];
    

    cell.likes_count.text = [NSString stringWithFormat:@"%ld万",[model.likes_count integerValue]/10000];
    
    cell.comments_count.text = [NSString stringWithFormat:@"%@",model.comments_count];
    
    [cell.focus setTitle:@"+关注" forState:UIControlStateNormal];
    
    [cell.focus setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cell.focus.backgroundColor = [UIColor colorWithRed:1 green:0.838 blue:0 alpha:1.000];
    cell.focus.titleLabel.font = [UIFont systemFontOfSize:15];
    cell.focus.layer.cornerRadius = 10;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MoreComicModel *model = self.topics_Arr[indexPath.row];
    
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    detailVC.idNumber = [NSString stringWithFormat:@"%@",model.idNUmber];
        
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (void)clickleft:(UIButton *)button{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
